<!DOCTYPE html>
<html>
<head>
    <title>机构管理</title>
    <meta charset="utf-8">
    <!-- 指定以IE8的方式来渲染 -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="-1">
    <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="/css/jsherp.css"/>
    <script type="text/javascript" src="/js/jquery-1.8.0.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css"/>
    <link type="text/css" rel="stylesheet" href="/css/common.css"/>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common/common.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
<!-- 数据显示table -->
<div id="tablePanel" class="easyui-panel" data-options="fit:true" style="padding:1px;top:300px;" title="机构列表" iconCls="icon-list">
    <table id="tableData" style="top:300px;border-bottom-color:#FFFFFF">
    </table>
</div>
<div id="organizationDlg" class="easyui-dialog" style="width:380px;padding:10px 20px"
     closed="true" buttons="#dlg-buttons" modal="true" cache="false" collapsible="false" closable="true">
    <form id="organizationFM" method="post" novalidate>
        <table>
            <tr>
                <td>上级机构</td>
                <td style="padding:5px">
                    <input name="orgParentName" id="orgParentName" class="easyui-textbox" style="width: 200px;" readonly="readonly"/>
                    <a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id="lookForSelectOrganization">
                        <span class="l-btn-left"><span class="l-btn-text icon-search l-btn-icon-left" style="height:20px;"></span></span></a>
                    <input name="orgParentNo" id="orgParentNo" type="hidden"/>
                    <input name="selectType" id="selectType" type="hidden"/>
                </td>
            </tr>
            <tr>
                <td>全称</td>
                <td style="padding:5px">
                    <input name="orgFullName" id="orgFullName" class="easyui-textbox"
                           data-options="required:true,validType:'length[2,30]'" style="width: 200px;"/>
                    <input name="id" id="id" type="hidden"/>
                </td>
            </tr>
            <tr>
                <td>简称</td>
                <td style="padding:5px">
                    <input name="orgAbr" id="orgAbr" class="easyui-textbox"
                           data-options="required:true,validType:'length[2,20]'" style="width: 200px;"/>
                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td style="padding:5px">
                    <select name="orgStcd" id="orgStcd" class="easyui-combobox"
                            data-options="panelHeight:'auto'" style="width:200px;">
                        <option value="1">未营业</option>
                        <option value="2">正常营业</option>
                        <option value="3">暂停营业</option>
                        <option value="4">终止营业</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>编号</td>
                <td style="padding:5px">
                    <input name="orgNo" id="orgNo" class="easyui-textbox"
                           data-options="required:true,validType:'length[2,20]'" style="width: 200px;"/>
                </td>
            </tr>
            <tr>
                <td>排序</td>
                <td style="padding:5px">
                    <input name="sort" id="sort"  class="easyui-textbox"
                           data-options="required:true,validType:'length[2,20]'" style="width: 200px;"/>
                </td>
            </tr>
            <tr>
                <td>开创时间</td>
                <td style="padding:5px">
                    <input type="text" name="orgCreateTime" id="orgCreateTime" class="easyui-datetimebox" style="width:200px;"/>
                </td>
            </tr>
            <tr>
                <td>停业时间</td>
                <td style="padding:5px">
                    <input type="text" name="orgStopTime" id="orgStopTime" class="easyui-datetimebox" style="width:200px;"/>
                </td>
            </tr>
            <tr>
                <td>备注</td>
                <td style="padding:5px">
                    <input name="remark" id="remark" class="easyui-textbox" data-options="multiline:true"
                           placeholder="暂无备注信息" style="width: 200px; height:40px;"/>
                </td>
            </tr>

        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" id="saveOrganization" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:void(0)" id="cancelOrganization" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#organizationDlg').dialog('close')">取消</a>
</div>
<div id="forSelectOrganizationDlg" class="easyui-dialog" style="width:380px;padding:10px 20px"
     closed="true" modal="true" cache="false" collapsible="false" closable="true">
</div>

<script type="text/javascript">
    var url;
    //初始化界面
    $(function () {
        var id=$("#id").val();
        var treeHeight=webH-35;
        //初始化系统基础信息
        $('#tableData').datagrid({
            height: treeHeight,
            toolbar: [
                {
                    id: 'addOrganization',
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        addOrganization();
                    }
                },
                '-',
                {
                    id: 'deleteOrganization',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        batDeleteOrganization();
                    }
                }
            ],
        });
        $('#tableData').prev('.datagrid-view2').find(".datagrid-body").append("<ul id='tt'><ul>");
        $('#tt').tree({
            url: '/organization/getOrganizationTree?id='+id,
            animate: true,
            checkbox: true,
            onDblClick: function(node){
                //双击修改
                editOrganization(node);
            }
        });
    });

    function addOrganization() {
        if(checkPower()){
            return;
        }
        $('#organizationFM').form('clear');
        $('#organizationDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;增加机构信息');
        $(".window-mask").css({width: webW, height: webH});
        //机构父级选择
        $("#selectType").val("orgParent");
        url = '/organization/addOrganization';
    }
    function editOrganization(node) {
        var id=node.id;
        $.ajax({
            type: "post",
            url: '/organization/findById',
            dataType: "json",
            data: ({
                id: id
            }),
            success: function(res) {
                if(res) {
                    if(res.code!=200){
                        $.messager.alert('提示', res.msg, 'error');
                        return;
                    }
                    $('#organizationFM').form('clear');
                    $('#organizationDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;修改机构信息');
                    $(".window-mask").css({width: webW, height: webH});
                    $("#orgAbr").textbox("setValue", res.data.orgAbr).focus();
                    $("#orgParentName").textbox("setValue", res.data.orgParentName);
                    $("#orgParentNo").val(res.data.orgParentNo);
                    $("#orgFullName").textbox("setValue", res.data.orgFullName);
                    $("#orgStcd").combobox("setValue", res.data.orgStcd);
                    $("#orgNo").textbox("setValue", res.data.orgNo);
                    $("#sort").textbox("setValue", res.data.sort);
                    $("#orgCreateTime").textbox("setValue", res.data.orgCreateTime);
                    $("#orgStopTime").textbox("setValue", res.data.orgStopTime);
                    $("#remark").textbox("setValue", res.data.remark);
                    $("#id").val(res.data.id);
                    //机构父级选择
                    $("#selectType").val("orgParent");
                    url = '/organization/editOrganization';
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '修改机构信息异常，请稍后再试！', 'error');
                return;
            }
        });

    }

    //批量删除机构信息
    function batDeleteOrganization() {
        var nodes = $('#tt').tree('getChecked');
        if (nodes.length == 0) {
            $.messager.alert('删除提示', '没有记录被选中！', 'info');
            return;
        }
        if (nodes.length > 0) {
            $.messager.confirm('删除确认', '确定要删除选中的' + nodes.length + '条机构信息吗？', function (r) {
                if (r) {
                    var ids = "";
                    for (var i = 0; i < nodes.length; i++) {
                        if (i == nodes.length - 1) {
                            ids += nodes[i].id;
                            break;
                        }
                        //alert(row[i].id);
                        ids += nodes[i].id + ",";
                    }
                    $.ajax({
                        type: "post",
                        url: "/organization/batchDeleteOrganization",
                        dataType: "json",
                        async: false,
                        data: ({
                            ids: ids
                        }),
                        success: function (res) {
                            if(res.code!=200){
                                $.messager.alert('提示', res.msg, 'error');
                                return;
                            }
                                $('#tt').tree('reload');
                        },
                        //此处添加错误处理
                        error: function () {
                            $.messager.alert('删除提示', '删除机构信息异常，请稍后再试！', 'error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //保存信息
    $("#saveOrganization").off("click").on("click", function () {
        if (!$("#orgFullName").val()) {
            $.messager.alert('提示', '全称不能为空！', 'warning');
            return;
        }
        if (!$("#orgAbr").val()) {
            $.messager.alert('提示', '简称不能为空！', 'warning');
            return;
        }
        if (!$("#orgNo").val()) {
            $.messager.alert('提示', '编号不能为空！', 'warning');
            return;
        }
        /**
         * create by: qiankunpingtai
         * create time: 2019/3/21 14:56
         * website：https://qiankunpingtai.cn
         * description:
         *  提交时需要对表格内容进行校验，校验通过后提交后台处理
         */
        var isValid = $("#organizationFM").form('validate');
        if(!isValid){
            return;
        }

        var objInfo = $("#organizationFM").serializeObject();
        $.ajax({
            type: "post",
            url: url,
            dataType: "json",
            data: ({
                info: JSON.stringify(objInfo)
            }),
            success: function(res) {
                if(res) {
                    if(res.code!=200){
                        $.messager.alert('提示', res.msg, 'error');
                        return;
                    }
                    $('#organizationDlg').dialog('close');
                    //加载完以后重新初始化
                    $('#tt').tree('reload');
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '保存机构信息异常，请稍后再试！', 'error');
                return;
            }
        });
    });
    //查询父级机构信息
    $("#lookForSelectOrganization").on("click", function () {
        $('#forSelectOrganizationDlg').dialog({
            title: '机构选择',
            width: webW/2,
            height: webH/2,
            closed: false,
            cache: false,
            href: '/pages/manage/organization_forselect.html',
            modal: true,
            resizable:true
        });
    });

</script>
</body>
</html>